package com.sangfor.ssl.service.utils.xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/* loaded from: classes2.dex */
public class XmlReader {
    private Object traverseElement(Node node) {
        ArrayList arrayList;
        HashMap hashMap = new HashMap();
        NodeList childNodes = node.getChildNodes();
        int length = childNodes.getLength();
        String str = length == 0 ? "" : null;
        int i = 0;
        for (int i2 = 0; i2 < length; i2++) {
            Node item = childNodes.item(i2);
            if (item.getNodeType() == 1) {
                String tagName = ((Element) item).getTagName();
                Object traverseElement = traverseElement(item);
                Object obj = hashMap.get(tagName);
                if (obj != null) {
                    if (obj instanceof ArrayList) {
                        arrayList = (ArrayList) obj;
                    } else {
                        ArrayList arrayList2 = new ArrayList();
                        arrayList2.add(obj);
                        hashMap.put(tagName, arrayList2);
                        arrayList = arrayList2;
                    }
                    arrayList.add(traverseElement);
                } else {
                    hashMap.put(tagName, traverseElement);
                }
            } else if (item.getNodeType() == 3) {
                if (length <= 1) {
                    str = item.getNodeValue();
                }
            } else if (item.getNodeType() == 4) {
                str = ((CDATASection) item).getData();
            }
        }
        NamedNodeMap attributes = node.getAttributes();
        boolean z = false;
        while (i < attributes.getLength()) {
            Attr attr = (Attr) attributes.item(i);
            hashMap.put(attr.getName(), attr.getValue());
            i++;
            z = true;
        }
        if (str != null) {
            if (!z) {
                return str;
            }
            hashMap.put("text", str);
        }
        return hashMap.values().size() == 0 ? "" : hashMap;
    }

    private Map<String, Object> parseContent(String str) throws SAXException, IOException, ParserConfigurationException {
        Document parse = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(str.getBytes()));
        Object traverseElement = traverseElement(parse.getDocumentElement());
        HashMap hashMap = new HashMap();
        hashMap.put(parse.getDocumentElement().getTagName(), traverseElement);
        return hashMap;
    }

    public static Map<String, Object> parse(String str) {
        try {
            return new XmlReader().parseContent(str);
        } catch (IOException | ParserConfigurationException | SAXException | Exception unused) {
            return null;
        }
    }
}
